﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;


using SampleTryouts.Models;
using System.Linq;
using Moq;
// unit test to test the LinqValueCalculater Object, that uses another object called MinimumDiscountHelper
// This is done to understand the power of Moq. The MS equivalent of fake can be used too.

namespace SampleTryouts.Tests
{
    [TestClass]
    public class UnitTest2
    {
        private Product[] products = { 
                                        new Product {Name = "Kayak", Category = "Water sports", Price = 275M},
                                        new Product {Name = "Lifejacket", Category = "Water sports", Price = 48.95M},
                                        new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
                                        new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
                                     };
        
        
        [TestMethod]
        public void Sum_Products_Correctly()
        {
            // arrange
            //IDiscountHelper discounter = new MinimumDiscountHelper(); 
            //var linqValCalc = new LinqValueCalculator(discounter);
            // commented out so that our unit test will use Moq to simulate the IDiscountHelper.ApplyDiscount

            Mock<IDiscountHelper> moq = new Mock<IDiscountHelper>();
            var outDesc = "Amount more than $100, so 10% discount applied";
            //moq.Setup(m => m.ApplyDiscount(378.40M, out outDesc)).Returns(340.56M);
            moq.Setup(m => m.ApplyDiscount(It.Is<decimal>(v=> v >100))).Returns<decimal>(t => (t*0.9M)); // mocking up the apply discount
            var linqValCalc = new LinqValueCalculator(moq.Object);  
            


            // act
            var result = linqValCalc.ValueProducts(products);

            var expectedTotal = 0.9M * products.Sum(p => p.Price);
            //assert
            Assert.AreEqual(expectedTotal, result);
        }
    }
}
